/*
 * Projet industriel MBDS 2012 VAMP
 */

package com.vampmobile.android.activity;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.vampmobile.R;
import com.vampmobile.android.VampCommon.Commons.KINDS;
import com.vampmobile.android.db.VampREST;
import com.vampmobile.android.tools.ActivityParameters;
import com.vampmobile.android.tools.ApplicationMenu;

/**
 * Class to display home page FO && AML
 * 
 */
public class Home extends Activity {
	public static final String TAG = "Home";
	private ImageView mProfile;
	private ImageView mThemes;
	private ImageView mMaps;
	private ImageView mNFCControle;
	private ImageView mList;
	private ImageView mMusics;
	private ImageView mHomeTitle;
	private ImageView mHomeButton;
	private ImageView mBackButton;
	private ImageView mSearchButton;
	private ImageView mQuitButton;
	private ImageView mAccountButton;

	@Override
	public void onConfigurationChanged(Configuration newConfig) {    
		super.onConfigurationChanged(newConfig);    
		initLayout();
	}	
	/**
	 * Called when the activity is first created. Responsible for initializing
	 * the UI.
	 */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setTitle(TAG);
		setContentView(R.layout.home);
		initLayout();
	}
	
	public void initLayout() {
		View myView = findViewById(R.id.home_grid1);
		if (myView==null)
			myView = findViewById(R.id.home_grid2);
		if (myView!=null) {
	        ViewGroup parent = (ViewGroup) myView.getParent();
	        parent.removeView(myView);
		}

        //Get Refer	    home.removeAllViews(); 
		if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){ 
		    // Paysage 
			GridLayout grd2 = (GridLayout) findViewById(R.id.home_grid2);
			if (grd2 == null){
	            //Inflate theLayout Information View 
				LinearLayout home = (LinearLayout) findViewById(R.id.home_layout_grid); 
	            View grd = getLayoutInflater().inflate(R.layout.home_grid_land, home, false);
	            home.addView(grd);
	        }
		} else {
			GridLayout grd1 = (GridLayout) findViewById(R.id.home_grid1);
			if (grd1 == null){
	            //Inflate theLayout Information View 
				LinearLayout home = (LinearLayout) findViewById(R.id.home_layout_grid); 
	            View grd = getLayoutInflater().inflate(R.layout.home_grid, home, false);
	            home.addView(grd);
	        }
		}
		// button of title bar
		mHomeTitle = (ImageView) findViewById(R.id.home_title);
		mHomeTitle.setOnClickListener(new ItemClickListener(KINDS.TOOLBAR));
		// button of toolbar
		mHomeButton = (ImageView) findViewById(R.id.home);
		mHomeButton.setOnClickListener(new ItemClickListener(KINDS.HOME_TITLE));
		mBackButton = (ImageView) findViewById(R.id.back_button);
		mBackButton.setOnClickListener(new ItemClickListener(KINDS.BACK));
		mSearchButton = (ImageView) findViewById(R.id.search);
		mSearchButton.setOnClickListener(new ItemClickListener(KINDS.SEARCH));
		mQuitButton = (ImageView) findViewById(R.id.quit);
		mQuitButton.setOnClickListener(new ItemClickListener(KINDS.QUIT));
		mAccountButton = (ImageView) findViewById(R.id.account);
		mAccountButton.setOnClickListener(new ItemClickListener(KINDS.PROFILE_TITLE));		
		// list of themes
		mThemes = (ImageView) findViewById(R.id.theme_img);
		mThemes.setOnClickListener(new ItemClickListener(KINDS.THEMES));
		// page to display functionality of maps
		mMaps = (ImageView) findViewById(R.id.maps);
		mMaps.setOnClickListener(new ItemClickListener(KINDS.MAPS));
		// page to start and open vehicle NFC
		mNFCControle = (ImageView) findViewById(R.id.nfc);
		mNFCControle.setOnClickListener(new ItemClickListener(
				KINDS.NFC_CONTROLE));
		// All of list
		mList = (ImageView) findViewById(R.id.list);
		mList.setOnClickListener(new ItemClickListener(KINDS.POI));
		// MyMusicBox
		mMusics = (ImageView) findViewById(R.id.musics);
		mMusics.setOnClickListener(new ItemClickListener(KINDS.MUSICS));
		// Profile
		mProfile = (ImageView) findViewById(R.id.profile);
		mProfile.setOnClickListener(new ItemClickListener(KINDS.PROFILE));
	}

	public class ItemClickListener implements OnClickListener {
		KINDS kind;

		public ItemClickListener(KINDS kind) {
			super();
			this.kind = kind;
		}

		public void onClick(View v) {
			Log.i(TAG, "type =" + kind);
			FrameLayout toolbar = (FrameLayout) findViewById(R.id.toolbar); 
			switch (kind) {
			case TOOLBAR:
				if (toolbar.getVisibility()!=View.VISIBLE)
					toolbar.setVisibility(View.VISIBLE);
				else
					toolbar.setVisibility(View.GONE);
				break;
			case QUIT:
				android.os.Process.killProcess(android.os.Process.myPid());
				java.lang.System.exit(0);
				finish();
				break;
			case SEARCH:
				break;
			case BACK:
				if (toolbar.getVisibility()!=View.VISIBLE)
					toolbar.setVisibility(View.VISIBLE);
				else
					toolbar.setVisibility(View.GONE);
				break;
			case PROFILE_TITLE:
				Intent i = new Intent();
				i.setClass(getApplicationContext(), Profile.class);
				i.setAction(Intent.ACTION_VIEW);
				i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				finish();
				startActivity(i);
				break;
			case HOME_TITLE:
				Intent j = new Intent();
				j.setClass(getApplicationContext(), Home.class);
				j.setAction(Intent.ACTION_VIEW);
				j.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				finish();
				startActivity(j);
				finish();
				break;
			case THEMES:
				Intent k = new Intent();
				k.setClass(getApplicationContext(), Theme.class);
				k.setAction(Intent.ACTION_VIEW);
				k.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(k);
				finish();
				break;
			case POI:
				Intent l = new Intent();
				l.setClass(getApplicationContext(), Poi.class);
				l.setAction(Intent.ACTION_VIEW);
				l.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(l);
				finish();
				break;
			case MAPS:
				Intent m = new Intent();
				m.setClass(getApplicationContext(), SocialMap.class);
				m.setAction(Intent.ACTION_VIEW);
				m.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(m);
				finish();
				break;
			case PROFILE:
				Intent n = new Intent();
				n.setClass(getApplicationContext(), Profile.class);
				n.setAction(Intent.ACTION_VIEW);
				n.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(n);
				finish();
				break;
			case MUSICS:
				try {
					Intent intent = new Intent(Intent.ACTION_MAIN);
					intent.setComponent(new ComponentName("com.mymusicbox",
							"com.mymusicbox.android.activity.Login"));
					startActivity(intent);
				} catch (Exception e) {
					showDownloadMyMusicBoxDialog();
				}
				break;
			case NFC_CONTROLE:
				Intent o = new Intent();
				o.setClass(getApplicationContext(), Vehicle.class);
				o.setAction(Intent.ACTION_VIEW);
				o.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				startActivity(o);
				finish();
				break;
			}

		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		ApplicationMenu.onCreateOptionsMenu(this, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		ApplicationMenu.onOptionsItemSelected(this, item);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		ApplicationMenu.onPrepareOptionsMenu(this, menu);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK ) {
			android.os.Process.killProcess(android.os.Process.myPid());
			java.lang.System.exit(0);
			finish();
			return true;
		}
		return false;
	}
	/*
	 * show box to add comment choice
	 */
	void showDownloadMyMusicBoxDialog() {
		Log.i(TAG, "*** Show box add comment will showed**");
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Download");
		alert.setMessage("Download and install MyMusicBox application?");
		alert.setPositiveButton("Download", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse("http://amlesas.dyndns.biz:8082/mymusicboxrest/MyMusicBoxClient.apk")); 
			    startActivity(browse); 
				Toast.makeText(getApplicationContext(),
						"Please, wait while download is not finished!", Toast.LENGTH_LONG)
						.show();
				Intent intent = new Intent(Intent.ACTION_VIEW); 
				intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "MyMusicBoxClient.apk")), "application/vnd.android.package-archive"); 
				startActivity(intent);   
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// Canceled.
					}
				});

		alert.show();
	}


}
